package com.gree.scmservice.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gree.common.entity.SysLogininfor;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.StringUtils;
import com.gree.framework.entity.CusUserDetails;
import com.gree.framework.entity.SysUser;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.LogInfoDao;
import com.gree.scmservice.service.GscmSysLogininforService;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 180686 on 2021/3/22 14:32
 */
@Service
public class GscmSysLogininforServiceImpl implements GscmSysLogininforService {

    @Autowired
    private LogInfoDao logInfoDao;

    @Autowired
    private Dao dao;

    @Override
    public TablePageData<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
        Pager pager = dao.createPager(logininfor.getPageNum(), logininfor.getPageSize());
        logininfor.setBaseEntityNull();
        Cnd cnd = Cnd.from(dao, logininfor);
        Integer count = dao.count(SysLogininfor.class,cnd);
        pager.setRecordCount(count);
        List<SysLogininfor> sysLogininforList = dao.query(SysLogininfor.class,cnd,pager);
        return new TablePageData(sysLogininforList, pager);
    }

    @Override
    public TablePageData<Record> selectLogininforList(JSONObject jsonObject) {
        CusUserDetails cusUserDetails = SecurityUtils.getUser();
        SysUser sysUser = cusUserDetails.getUser();
        String comp = sysUser.getComp();
        String filter = "";
        if(!"".equals(jsonObject.getString("login_name")) && jsonObject.getString("login_name")!=null){
            filter+=" and login.login_name like '%"+jsonObject.getString("login_name")+"%'";
        }
        if(!"".equals(jsonObject.getString("ipaddr")) && jsonObject.getString("ipaddr")!=null){
            filter+=" and login.ipaddr like '"+jsonObject.getString("ipaddr")+"%'";
        }
        if(!"".equals(jsonObject.getString("comp")) && jsonObject.getString("comp")!=null){
            filter+=" and login.comp = '"+jsonObject.getString("comp")+"'";
        }
        if(!"".equals(jsonObject.getString("status")) && jsonObject.getString("status")!=null){
            filter+=" and login.status = '"+jsonObject.getString("status")+"'";
        }
        if(jsonObject.getJSONArray("dateRange")!=null){
            if(jsonObject.getJSONArray("dateRange").size()!=0){
                filter+=" and login.login_time between " +
                        "to_date('"+jsonObject.getJSONArray("dateRange").get(0)+"','yyyy-mm-dd') " +
                        "and to_date('"+jsonObject.getJSONArray("dateRange").get(1)+"','yyyy-mm-dd')";
            }
        }
        filter+=" order by login.id desc";
        int count = logInfoDao.getLoginLogCount(comp,filter);
        TablePageData<Record> recordTablePageData = logInfoDao.displayLoginLog(comp, filter, count, jsonObject.getIntValue("page"), jsonObject.getIntValue("pagesize"));
        return recordTablePageData;
    }

    @Override
    public int deleteLogininforByIds(Long[] infoIds) {
        return logInfoDao.deleteLogininforByIds(infoIds);
    }

    @Override
    public void cleanLogininfor() {
        logInfoDao.cleanLogininfor();
    }
}
